package com.yztc.xiami_app.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.user.MyUser;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.imageview_xina_login)
    ImageView imageviewXinaLogin;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    private boolean isShow;
    @BindView(R.id.imageview_back)
    ImageView backIv; //返回图标
    @BindView(R.id.textview_register_login)
    TextView textviewRegisterLogin;
    @BindView(R.id.login_userName_edt)
    EditText userNameEdt; //输入用户名
    @BindView(R.id.image_clean)
    ImageView imageClean;
    @BindView(R.id.login_psd_edt)
    EditText psdEdt; //输入密码
    @BindView(R.id.image_psw_show)
    ImageView imagePswShow;
    @BindView(R.id.image_psw_clean)
    ImageView imagePswClean;
    @BindView(R.id.checkbox_login)
    CheckBox checkboxLogin;
    @BindView(R.id.textview_login)
    TextView textviewLogin;
    @BindView(R.id.textview_forget_psw)
    TextView textviewForgetPsw;
    @BindView(R.id.imageview_forget_psw)
    ImageView imageviewForgetPsw;
    @BindView(R.id.login_btn)
    Button loginBtn; //登录按钮
    @BindView(R.id.imageview_qq_login)
    ImageView imageviewQqLogin;
    private UMAuthListener umAuthListener=new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        UMShareAPI.get(this);
        initView();
    }

    private void initView() {
        userNameEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //获得当前输入内容
                String number = userNameEdt.getEditableText().toString();

                if (TextUtils.isEmpty(number)) { //当没有输入内容的时候，清空按钮不可见
                    imageClean.setVisibility(View.INVISIBLE);
                } else {
                    imageClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        psdEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String psw = psdEdt.getEditableText().toString();
                if (TextUtils.isEmpty(psw)) { //当没有输入内容的时候，清空按钮不可见
                    imagePswClean.setVisibility(View.INVISIBLE);
                } else {
                    imagePswClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @OnClick({R.id.imageview_back, R.id.textview_register_login, R.id.login_userName_edt, R.id.image_clean,
            R.id.login_psd_edt, R.id.image_psw_show, R.id.image_psw_clean, R.id.checkbox_login,
            R.id.textview_login, R.id.textview_forget_psw, R.id.imageview_forget_psw, R.id.login_btn,
            R.id.imageview_qq_login, R.id.imageview_xina_login, R.id.activity_login})
    public void onClick(View view) {
        Intent intent = new Intent();
        UMShareAPI mShareAPI=null;
        switch (view.getId()) {
            case R.id.imageview_back:
                finish();
                break;
            case R.id.textview_register_login:
                intent.setClass(this, PhoneRegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.image_clean:
                //一键清空的操作
                userNameEdt.setText("");
                break;
            case R.id.login_psd_edt:
                break;
            case R.id.image_psw_show:
                //设置密码是否可见
                isShow = !isShow;
                setPassworldIsVisible(isShow, psdEdt);
                break;
            case R.id.image_psw_clean:
                //设置密码是一键清空
                psdEdt.setText("");
                break;
            case R.id.checkbox_login:
                break;
            case R.id.textview_login:
                break;
            case R.id.textview_forget_psw:
                intent.setClass(this, FindPasswardActivity.class);
                startActivity(intent);
                break;
            case R.id.imageview_forget_psw:
                intent.setClass(this, FindPasswardActivity.class);
                startActivity(intent);
                break;
            case R.id.login_btn:
                //登录
                login();
                break;
            case R.id.imageview_qq_login:
                //QQ登录
                mShareAPI= UMShareAPI.get( LoginActivity.this );
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                break;
            case R.id.imageview_xina_login:
                //新浪登录
                mShareAPI = UMShareAPI.get( LoginActivity.this );
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                break;
            case R.id.activity_login:
                break;
        }
    }

    //通过Bmob云登录
    private void login() {
        //Bomb登录
        String userName = userNameEdt.getEditableText().toString(); //获取用户名
        String psw = psdEdt.getEditableText().toString(); //获取密码
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(psw)) {
            BmobUser.loginByAccount(userName, psw, new LogInListener<MyUser>() {
                @Override
                public void done(MyUser user, BmobException e) {
                    if (user != null) {
                        //登录成功
                        EventBus.getDefault().postSticky(user); //发布事件，传递User对象到"用户个人信息界面"
                        //跳转到主页
                        finish(); //关闭登录画面
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或密码有误", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }


    }

    /**
     * 设置输入框密码是否可见
     *
     * @param isShow 输入框密码是否可见
     * @param pswEdt 输入框
     */
    private void setPassworldIsVisible(boolean isShow, EditText pswEdt) {
        if (isShow) { //可见
            imagePswShow.setImageResource(R.mipmap.switch_in_show);
            pswEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            imagePswShow.setImageResource(R.mipmap.switch_in_hide);
            pswEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        //切换后将EditText光标置于末尾
        CharSequence charSequence = pswEdt.getText();
        if (charSequence instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence;
            Selection.setSelection(spanText, charSequence.length());
        }
    }
}
